package edu.uoc.tds.pac4.client.ss1;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Vector;

import edu.uoc.tds.pac4.client.Client;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.client.ss2.Helpers;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.beans.Centre;
import edu.uoc.tds.pac4.beans.Universitat;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;

public class PantallaModCentres extends PantallaCentres implements PanelClientInterface{
	
	private static final long serialVersionUID = 1L;
	private JPanel jPanelContainer = null;
	private JPanelLlistatCentres jPanelLlistat = null;
	private JLabel jLabelUniversitat = null;
	private JComboBox jComboBoxUniversitat = null;
	private JButton jButtonCargar = null;
	private JButton jButtonModificar = null;
	private JButton jButtonEliminar = null;
	Vector<Centre> currentCentre = null;
	private Client cliente = null;
	
	/**
	 * Constructor
	 */
	public PantallaModCentres() {
		super();
		initialize();
	}
	
	/**
	 * Constructor que rep una referencia al servidor remot
	 * 
	 * @param remot
	 */
	public PantallaModCentres(Client cliente,  RemoteInterface remot) {
		this.cliente = cliente;
		this.remot = remot;
		initialize();
	}

	/**
	 * Inicialitza la pantalla
	 */
	protected void initialize() {
		// this.setTitle("Pantalla.title");
	}
	
	/**
	 * This method initializes jPanelContainer
	 * 
	 * @return javax.swing.JPanel
	 */
	@Override
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
			jPanelContainer = new JPanel();
			jPanelContainer.setSize(dimension);
			jPanelContainer.setLayout(new GridBagLayout());
			
			// Universitat
			GridBagConstraints gbcLblUniversitat = new GridBagConstraints();
			gbcLblUniversitat.gridx = 0;
			gbcLblUniversitat.gridy = 0;
			gbcLblUniversitat.weightx = 0.3;
			gbcLblUniversitat.anchor = GridBagConstraints.LINE_END;
			jLabelUniversitat = new JLabel("Universitat");	
			GridBagConstraints gbcComboBoxUniversitat = new GridBagConstraints();
			gbcComboBoxUniversitat.weightx = 0.7;
			gbcComboBoxUniversitat.gridx = 1;
			gbcComboBoxUniversitat.gridy = 0;
			gbcComboBoxUniversitat.insets = new Insets(5, 10, 5, 10);
			jPanelContainer.add(jLabelUniversitat, gbcLblUniversitat);
			jPanelContainer.add(getJComboBoxUniversitat(), gbcComboBoxUniversitat);
			
			
			jPanelContainer.add(getjButtonCargar(), null);			
			jPanelContainer.add(getjButtonModificar(), null);
			jPanelContainer.add(getjButtonEliminar(), null);
			
			GridBagConstraints gbcPanelCentre = new GridBagConstraints();
			gbcPanelCentre.gridx = 1;
			gbcPanelCentre.gridy = 1;
			gbcPanelCentre.weightx = 1;
			gbcPanelCentre.weighty = 1;
			gbcPanelCentre.insets = new Insets(0, 10, 10, 10);
			gbcPanelCentre.fill = GridBagConstraints.BOTH;

			jPanelContainer.add(getjPanelLlistat(), gbcPanelCentre);		
			
		}
		return jPanelContainer;
	}
	
	/**
	 * This method initializes jComboBoxUniversitat
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxUniversitat() {
		if (jComboBoxUniversitat == null) {
			jComboBoxUniversitat = new JComboBox();
			jComboBoxUniversitat.setMinimumSize(new Dimension(300, 20));
			jComboBoxUniversitat.setPreferredSize(new Dimension(400, 20));
			Vector<Universitat> universitats = carregarUniversitats();
			Helpers.setItemsCombo(universitats, jComboBoxUniversitat);			
		}
		return jComboBoxUniversitat;
	}
	
	/**
	 * This method initializes jPanelUsuari
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getjPanelLlistat() {
		if (jPanelLlistat == null) {
			jPanelLlistat = new JPanelLlistatCentres();
		}
		return jPanelLlistat;
	}
	
	/**
	 * Actualitza el contingut del llistat i la taula
	 */
	public void updateTaula() {
		
		
		// crida al servidor per rebre els events
		try {
			if (getUniversitat() == null)
				currentCentre = remot.getCentres(true);
			else
				currentCentre = remot.getCentres(getUniversitat(), true);
		} catch (RemoteException e) {
			new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
		} catch (GestioEventsException e) {
			new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
		}
		jPanelLlistat.drawCentres(currentCentre);
	}
	
	/**
	 * Retorna la universitat seleccionada
	 * 
	 * @return
	 */
	public Universitat getUniversitat() {
		Universitat currentUniversitat = null;
		if (jComboBoxUniversitat.getSelectedItem() instanceof Universitat)
			currentUniversitat = (Universitat) jComboBoxUniversitat.getSelectedItem();
		else
			currentUniversitat = null;

		return currentUniversitat;
	}
	
	/**
	 * This method initializes jButtonCargar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getjButtonCargar() {

		if (jButtonCargar == null) {
			jButtonCargar = new JButton("Cargar");
			jButtonCargar.setBounds(new Rectangle(85, 195, 100, 25));
			jButtonCargar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		

					try {
						updateTaula();

					} catch (Exception e){
						// Pending verificar
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButtonCargar;
	}
	
	/**
	 * This method initializes jButtonModificar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getjButtonModificar() {

		if (jButtonModificar == null) {
			jButtonModificar = new JButton("Modificar");
			jButtonModificar.setBounds(new Rectangle(85, 195, 100, 25));
			jButtonModificar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		

					try {
						new PantallaModificacioCentres(cliente, remot,currentCentre.get(jPanelLlistat.getSelectedRow()));
					} catch (Exception e){
						// Pending verificar
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButtonModificar;
	}
	
	/**
	 * This method initializes jButtonEliminar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getjButtonEliminar() {

		if (jButtonEliminar == null) {
			jButtonEliminar = new JButton("Eliminar");
			jButtonEliminar.setBounds(new Rectangle(85, 195, 100, 25));
			jButtonEliminar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		

					try {
						Centre centre = currentCentre.get(jPanelLlistat.getSelectedRow());
						centre.baixa(new Date(), "Eliminat desde pantalla");
						remot.insertUpdateCentre(centre, false);
						new PantallaError("Info_baixa", PantallaError.Tipus.INFO);  

					} catch (Exception e){
						// Pending verificar
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButtonEliminar;
	}
}
